// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoMovingObject;
import zombie.iso.IsoWorld;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.ServerMap;
import zombie.network.packets.INetworkPacket;
import zombie.vehicles.BaseVehicle;
import zombie.vehicles.VehicleManager;

public class MovingObject implements INetworkPacket {
    public final byte objectTypeNone = 0;
    public final byte objectTypePlayer = 1;
    public final byte objectTypeZombie = 2;
    public final byte objectTypeVehicle = 3;
    public final byte objectTypeObject = 4;
    private boolean isProcessed = false;
    private byte objectType = 0;
    private short objectId;
    private int squareX;
    private int squareY;
    private byte squareZ;
    private IsoMovingObject object;

    public void setMovingObject(IsoMovingObject value) {
        this.object = value;
        this.isProcessed = true;
        if (this.object == null) {
            this.objectType = 0;
            this.objectId = 0;
        } else if (this.object instanceof IsoPlayer) {
            this.objectType = 1;
            this.objectId = ((IsoPlayer)this.object).getOnlineID();
        } else if (this.object instanceof IsoZombie) {
            this.objectType = 2;
            this.objectId = ((IsoZombie)this.object).getOnlineID();
        } else if (this.object instanceof BaseVehicle) {
            this.objectType = 3;
            this.objectId = ((BaseVehicle)this.object).VehicleID;
        } else {
            IsoGridSquare isoGridSquare = this.object.getCurrentSquare();
            this.objectType = 4;
            this.objectId = (short)isoGridSquare.getMovingObjects().indexOf(this.object);
            this.squareX = isoGridSquare.getX();
            this.squareY = isoGridSquare.getY();
            this.squareZ = (byte)isoGridSquare.getZ();
        }
    }

    public IsoMovingObject getMovingObject() {
        if (!this.isProcessed) {
            if (this.objectType == 0) {
                this.object = null;
            }

            if (this.objectType == 1) {
                if (GameServer.bServer) {
                    this.object = (IsoMovingObject)GameServer.IDToPlayerMap.get(this.objectId);
                } else if (GameClient.bClient) {
                    this.object = (IsoMovingObject)GameClient.IDToPlayerMap.get(this.objectId);
                }
            }

            if (this.objectType == 2) {
                if (GameServer.bServer) {
                    this.object = ServerMap.instance.ZombieMap.get(this.objectId);
                } else if (GameClient.bClient) {
                    this.object = GameClient.IDToZombieMap.get(this.objectId);
                }
            }

            if (this.objectType == 3) {
                this.object = VehicleManager.instance.getVehicleByID(this.objectId);
            }

            if (this.objectType == 4) {
                IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(this.squareX, this.squareY, this.squareZ);
                if (isoGridSquare == null) {
                    this.object = null;
                } else {
                    this.object = (IsoMovingObject)isoGridSquare.getMovingObjects().get(this.objectId);
                }
            }

            this.isProcessed = true;
        }

        return this.object;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.objectType = b.get();
        this.objectId = b.getShort();
        if (this.objectType == 4) {
            this.squareX = b.getInt();
            this.squareY = b.getInt();
            this.squareZ = b.get();
        }

        this.isProcessed = false;
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putByte(this.objectType);
        b.putShort(this.objectId);
        if (this.objectType == 4) {
            b.putInt(this.squareX);
            b.putInt(this.squareY);
            b.putByte(this.squareZ);
        }
    }

    @Override
    public int getPacketSizeBytes() {
        return this.objectType == 4 ? 12 : 3;
    }

    @Override
    public String getDescription() {
        String string = "";
        switch (this.objectType) {
            case 0:
                string = "None";
                break;
            case 1:
                string = "Player";
                break;
            case 2:
                string = "Zombie";
                break;
            case 3:
                string = "Vehicle";
                break;
            case 4:
                string = "NetObject";
        }

        return this.objectType == 4
            ? "\n\tMovingObject [type="
                + string
                + "("
                + this.objectType
                + ") | id="
                + this.objectId
                + " | position=("
                + this.squareX
                + ", "
                + this.squareY
                + ", "
                + this.squareZ
                + ") ]"
            : "\n\tMovingObject [type=" + string + "(" + this.objectType + ") | id=" + this.objectId + "]";
    }
}
